﻿//////////////////////////////////////////////////////////////////////////////////////////////////////
//Name: Bartosz
//FullName: Adamczewski
//Project: InternalClasses
//Component: InternalClasses
//StartDate: 2009-08-17 19:29:04
//EndDate: 2009-08-17 19:29:04
//Description: The implementation of InternalClasses component.
//Rights:  Copyright (c) Bartosz Adamczewski
/////////////////////////////////////////////////////////////////////////////////////////////////////

#region imports.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#endregion 

namespace InternalClasses.Validation
{
    /// <summary>
    /// The Method Validator, that provies
    /// mechanisms for method parameter validation.
    /// </summary>
    public class MethodValidator
    {
        /// <summary>
        /// Checks if the Assert(s) is Instance Of the value type.
        /// </summary>
        /// <param name="value">the value.</param>
        /// <param name="type">the type.</param>
        public static void IsInstanceOf<TAssert>(TAssert value, Type type) where TAssert : class
        {
            if (value.GetType() != type)
            {
                CurrentAssertFail(null);
            }
        }

        /// <summary>
        /// Checks if the Assert(s) is Not Null.
        /// </summary>
        /// <param name="value">the value.</param>
        public static void IsNotNull<TAssert>(TAssert value) where TAssert : class
        {
            IsTrue(value != null);
        }

        /// <summary>
        /// Checks if the Assert(s) is Not Null.
        /// </summary>
        /// <param name="value1">the value.</param>
        /// <param name="value2">the value.</param>
        public static void IsNotNull<TAssert1, TAssert2>(TAssert1 value1, TAssert2 value2)
            where TAssert1 : class
            where TAssert2 : class
        {
            IsNotNull(value1);
            IsNotNull(value2);
        }

        /// <summary>
        /// Checks if the Assert(s) is Not Null Or Empty.
        /// </summary>
        /// <param name="value">the value.</param>
        public static void IsNotNullOrEmpty(System.Collections.IList value)
        {
            IsNotNull(value);
            IsTrue(value.Count > 0);
        }

        /// <summary>
        /// Checks if the Assert(s) is Not Null Or Empty.
        /// </summary>
        /// <param name="value">the value.</param>
        public static void IsNotNullOrEmpty(string value)
        {
            IsNotNull(value);
            IsTrue(value.Length > 0);
        }

        /// <summary>
        /// Checks if the Assert(s) is Null.
        /// </summary>
        /// <param name="value">the value.</param>
        public static void IsNull<TAssert>(TAssert value) where TAssert : class
        {
            IsTrue(value == null);
        }

        /// <summary>
        /// Checks that a given Assert is not Null.
        /// </summary>
        /// <param name="values">the values.</param>
        public static void NotNullElements<TAssert>(IEnumerable<TAssert> values) where TAssert : class
        {
            IsNotNull(values);

            foreach (TAssert value in values)
            {
                if (value == null)
                {
                    CurrentAssertFail(null);
                }
            }
        }

        /// <summary>
        /// Checks if the Assert(s) is False.
        /// </summary>
        /// <param name="condition">the condition.</param>
        public static void IsFalse(bool condition)
        {
            if (condition == true)
            {
                CurrentAssertFail(null);
            }
        }

        /// <summary>
        /// Checks if the Assert(s) is True.
        /// </summary>
        /// <param name="condition">the condition.</param>
        public static void IsTrue(bool condition)
        {
            if (condition == false)
            {
                CurrentAssertFail(null);
            }
        }

        /// <summary>
        /// Checks if the Assert(s) is True.
        /// </summary>
        /// <param name="condition">the condition.</param>
        /// <param name="message">the message.</param>
        public static void IsTrue(bool condition, string message)
        {
            if (condition == false)
            {
                CurrentAssertFail(message);
            }
        }

        /// <summary>
        /// Throws the Exception
        /// </summary>
        public static void CurrentAssertFail(string message)
        {
            throw new ValidatorException(message);
        }
    }
}
